import {
  req
} from '../pages/utils/request/request'
import {
  uniToast
} from '../pages/utils/uni/uni'
export default {
  namespaced: true,

  state: {
    /* 用户资料*/
    userInfo: JSON.parse(uni.getStorageSync("USERINFO") || '{}'),
    /* 用户登录凭证*/
    token: uni.getStorageSync("TOKEN") || '',
    /* 用户收货地址 */
    userAddress: JSON.parse(uni.getStorageSync("USERADDRESS") || '{}'),
  },

  mutations: {
    /* 用户选择地址 */
    USERCHOOSEADDRESS(state, idx) {
      state['userAddress']['addressIdx'] = idx
      this.commit("user/SAVEUSERADDRESS")
    },
    /* 获取用户收货地址*/
    UPDATEUSERADDRESS(state, address) {
      state.userAddress = address
      this.commit("user/SAVEUSERADDRESS")
    },
    /* 保存用户收货地址*/
    SAVEUSERADDRESS(state) {
      uni.setStorageSync("USERADDRESS", JSON.stringify(state.userAddress))
    },
    /* 更新token*/
    UPDATETOKEN(state, token) {
      state.token = token
      this.commit("user/SAVETOKEN")
    },
    /* 将token保存本地*/
    SAVETOKEN(state) {
      uni.setStorageSync("TOKEN", state.token)
    },
    /* 更新用户数据*/
    UPDATEUSERINFO(state, userinfo) {
      state.userInfo = userinfo
      this.commit("user/SAVEUSERINFO")
    },
    /* 将用户数据保存本地*/
    SAVEUSERINFO(state) {
      uni.setStorageSync("USERINFO", JSON.stringify(state.userInfo))
    },
    /* 用户资料编辑 */
    EDITUSERINFO(state, userinfo) {
      state.userInfo = {
        ...state.userInfo,
        ...userinfo
      }
      this.commit("user/SAVEUSERINFO")
    }
  },

  actions: {
    login({
      dispatch,
      commit
    }, data) {
      req('login', {
        reqData: data,
        method: 'POST'
      }).then(({
        data: res
      }) => {
        commit('UPDATETOKEN', res['token'])
        dispatch('getUserInfo')
      }).catch(() => { })
    },
    /* 向服务器获取用户信息 */
    getUserInfo({
      dispatch
    }) {
      req('findAccountInfo', {
        isToken: true
      }).then(({
        data: res
      }) => {
        dispatch('getMyData', res['result'][0])
      }).catch(err => { })
    },
    /* 获取我的页面数据 */
    getMyData({
      commit,
      dispatch
    }, data) {
      req('findMy', {
        isToken: true
      }).then(({
        data: res
      }) => {
        const obj = {
          ...data,
          ...res['result'][0]
        }
        obj['desc'] = obj['desc'] || '这个家伙很懒，什么也没有留下'
        commit("UPDATEUSERINFO", obj);
        dispatch('getAddress')
        uniToast("登录成功")
        this.timer = setTimeout(() => {
          uni.switchTab({
            url: '/pages/my/my'
          })
        }, 1000);
      }).catch(() => { })
    },
    /* 获取用户收货地址 */
    getAddress({
      commit
    }, addressIdx = 0) {
      req('findAddress', {
        isToken: true
      }).then(({
        data: res
      }) => {
        const obj = {
          addressIdx,
          addressData: res['result']
        }
        commit('UPDATEUSERADDRESS', obj)
      })
    },
    logout({
      commit
    }) {
      req('logout', {
        isToken: true,
        method: 'POST'
      }).then(({
        data: res
      }) => {
        if (res['code'] === 'F001') {
          commit("UPDATEUSERINFO", {}) //清空用户数据
          commit("UPDATETOKEN", "") //清空登录凭证
          commit("UPDATEUSERADDRESS", {}) //清空用户收货地址
          this.timer = setTimeout(() => {
            uni.switchTab({
              url: '/pages/index/index'
            })
          }, 500)
        }
        uniToast(res['msg'])
      })
    },
    destroyData({
      commit
    }) {
      commit("UPDATEUSERINFO", {}) //清空用户数据
      commit("UPDATETOKEN", "") //清空登录凭证
      commit("UPDATEUSERADDRESS", {}) //清空用户收货地址
    },

  },

  getters: {
    /* 计算用户当前选择的地址是 */
    curAddress(state) {
      return state['userAddress']['addressData'][state['userAddress']['addressIdx']]
    },
    /* 计算当前用户是否登录 */
    isLogin(state) {
      return state['token'] && state['userInfo'] ? true : false
    }
  }
}
